package com.gaoyi.crm.control.popup;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.gaoyi.crm.R;
import com.gaoyi.crm.constant.Constants;
import com.gaoyi.crm.constant.EventCode;
import com.gaoyi.crm.constant.SPName;
import com.gaoyi.crm.databinding.PopupActionbarBinding;
import com.gaoyi.crm.db.bean.Event;
import com.gaoyi.crm.util.DensityUtil;
import com.gaoyi.crm.util.SPUtil;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.util.Utils;

import org.greenrobot.eventbus.EventBus;


public class MyPopupWindow extends BasePopupWindow<PopupActionbarBinding> {

    private Drawable mDrawableCheck;
    private OnDropDownClick mOnDropDownClick;

    public MyPopupWindow(@NonNull Context context ,OnDropDownClick onDropDownClick) {
        super(context);
        mOnDropDownClick = onDropDownClick;
        setWidth(DensityUtil.dp2px(180));
        setOutsideTouchable(true);
    }

    @Override
    protected void initView() {
        MyOnClickListener onClickListener = new MyOnClickListener();
        mBinding.tvLogout.setOnClickListener(onClickListener);
        mBinding.tvOffline.setOnClickListener(onClickListener);
        mBinding.tvOnline.setOnClickListener(onClickListener);
        mBinding.tvHomePage.setOnClickListener(onClickListener);
        mBinding.tvSetting.setOnClickListener(onClickListener);
    }

    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (SPUtil.getInstance().getString(SPName.ID).isEmpty()) {
                ToastUtil.showToast(R.string.p_login);
                return;
            }
            int id = view.getId();
            if (id == R.id.tvLogout) {
                logout();
            } else if (id == R.id.tvOffline) {
                changeNetState(Constants.OFFLINE);
            } else if (id == R.id.tvOnline) {
                changeNetState(Constants.ONLINE);
            } else if (id == R.id.tvHomePage) {
                EventBus.getDefault().post(new Event(EventCode.EVENT_FRAGMENT));
            } else if (id == R.id.tvSetting) {

            }
            dismiss();
        }
    }

    private void logout() {
        if (mOnDropDownClick != null) {
            mOnDropDownClick.logout();
        }
    }

    private void changeNetState(int state) {
        Utils.changeNetStatus(state);
    }

    public void setNetState(int state) {
        if (mDrawableCheck == null) {
            mDrawableCheck = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_baseline_check, null);
            if (mDrawableCheck != null) {
                mDrawableCheck.setBounds(0, 0, mDrawableCheck.getIntrinsicWidth(), mDrawableCheck.getIntrinsicHeight());
            }
        }
        mBinding.tvOffline.setCompoundDrawablesRelative(state == 0 ? mDrawableCheck : null, null, null, null);
        mBinding.tvOnline.setCompoundDrawablesRelative(state == 0 ? null : mDrawableCheck, null, null, null);

    }

    @Override
    protected PopupActionbarBinding getViewBinding() {
        return PopupActionbarBinding.inflate(LayoutInflater.from(mContext));
    }

    public interface OnDropDownClick{
        void logout();
    }
}
